/*
 * @Author: your name
 * @Date: 2021-06-27 19:08:22
 * @LastEditTime: 2021-06-30 00:45:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ttstar\assets\scripts\game\module\home\HomeWindow.ts
 */
import { instantiate } from 'cc';
import { GProgressBar, GTextField } from 'fairygui-cc';
import { LayerType } from '../../../frameworks/base/LayerType';
import { UIWindow } from '../UIWindow';


@uiBind('fgui/loading', 'LoadingWindow')
export class LoadingWindow extends UIWindow {

    @uiInject('text')
    protected text!: GTextField;

    @uiInject('progress')
    protected progress!: GProgressBar;

    private curProgress: number;
    private showProgress: number;
    private delta: number;

    public constructor() {
        super();
        this.layer = LayerType.UI_Popup;
    }

    /**
     * 第一次启动的时候回调用
     */
    protected onStart() {
        this.makeFullScreen();

        this.progress.min = 0;
        this.progress.max = 100;
        this.curProgress = 0;
        this.showProgress = 0;
        this.delta = 0;

        this.text.text = `${Math.floor(this.curProgress)}%`;
        this.progress.value = this.showProgress;
    }

    protected update(dt: number) {
        this.delta += dt;
        if (this.delta >= 50) {
            this.delta = 0;
            this.showProgress += 1;
            if (this.showProgress > this.curProgress) {
                this.showProgress = this.curProgress;
            }
            this.progress.value = this.showProgress;
        }
    }

    public setProgress(p: number) {
        this.curProgress = p;
        if (this.curProgress > 100) {
            this.curProgress = 100;
        }
    }
}